package com.sskj.fca.ui.activity;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.jakewharton.rxbinding2.widget.RxAdapterView;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.fca.R;
import com.sskj.fca.bean.CommonBean;
import com.sskj.fca.presenter.RechargePresenter;
import com.sskj.fca.ui.ArrowAdapter;

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;

@Route(path = RConfig.APP_RECHARGE)//充值
public class RechargeActivity extends BaseActivity<RechargePresenter> {

    @BindView(R.id.tvDesc)
    TextView tvDesc;
    @BindView(R.id.et_recharge_price)
    EditText etRechargePrice;
    @BindView(R.id.btn_recharge)
    Button btnRecharge;
    @BindView(R.id.spinner)
    Spinner spinner;

    private String canMin;//最低提现
    private String beiShu;//提现倍数
    private String bibaoMin;//币宝提现最低额

    private int pay_way;

    @Override
    protected int getLayoutResId() {
        return R.layout.app_activity_recharge;
    }

    @Override
    public RechargePresenter getPresenter() {
        return new RechargePresenter();
    }

    @SuppressLint("CheckResult")
    @Override
    protected void initView() {
        setTitle("充值");
        initSpinner();
        spinner.setEnabled(false);
        RxAdapterView.itemSelections(spinner)
                .subscribe(pos -> {
                    pay_way = pos;
                    if (TextUtils.isEmpty(canMin) || TextUtils.isEmpty(beiShu) || TextUtils.isEmpty(bibaoMin)) {
                        return;
                    }
                    switch (pos) {
                        case 0:
                            etRechargePrice.setHint("请输入充值数量，不可小于" + canMin + ",且为" + beiShu + "的倍数");
                            break;
                        case 1:
                            etRechargePrice.setHint("请输入充值数量，不可小于" + bibaoMin);
                            break;
                    }
                });
        mPresenter.getCommonData();
//        mPresenter.getRecharge();
        ClickUtil.click(btnRecharge, () -> {
            if (TextUtils.isEmpty(etRechargePrice.getText().toString().trim())) {
                ToastUtil.showShort("请输入充值数量");
                return;
            }
            switch (pay_way) {
                case 0:
                    if (Float.valueOf(etRechargePrice.getText().toString().trim()) < Float.valueOf(canMin)) {
                        ToastUtil.showShort("充值数量不能小于" + canMin);
                        return;
                    }
                    if (Float.valueOf(etRechargePrice.getText().toString().trim()) % Float.valueOf(beiShu) != 0) {
                        ToastUtil.showShort("充值数量需为" + beiShu + "的倍数");
                        return;
                    }
                    mPresenter.getRechargeUrl(etRechargePrice.getText().toString().trim());
                    break;
                case 1:
                    if (Float.valueOf(etRechargePrice.getText().toString().trim()) < Float.valueOf(bibaoMin)) {
                        ToastUtil.showShort("充值数量不能小于" + bibaoMin);
                        return;
                    }
                    mPresenter.getRechargeUrlTwo(etRechargePrice.getText().toString().trim());
                    break;
            }
        });
    }

    public void updataUI(CommonBean commonBean) {
        if (commonBean == null) {
            return;
        }
        canMin = commonBean.getBuy_info().getRecharge().getQianshu1();
        beiShu = commonBean.getBuy_info().getRecharge().getBeishu1();
        bibaoMin = commonBean.getBuy_info().getRecharge().getBibao_min();
        tvDesc.setText(commonBean.getBuy_info().getRecharge().getRecharge());
        etRechargePrice.setHint("请输入充值数量，不可小于" + canMin + ",且为" + beiShu + "的倍数");

    }

    public void rechargeSuccess(String rechargeUrl) {
        ARouter.getInstance().build(RConfig.APP_RECHATRGE_WEBVIEW)
                .withString(Constans.RECHARGE_URL, rechargeUrl)
                .withString(Constans.WEBVIEW_TYPE, "充值")
                .navigation();
    }

    private void initSpinner() {
        List<String> payNames = Arrays.asList(getResources().getStringArray(R.array.app_recharge_types));
        ArrowAdapter spinnerAdapter = new ArrowAdapter(this,
                R.layout.app_item_rank_type, payNames);
        spinnerAdapter.setDropDownViewResource(R.layout.mine_item_pay_type_drop);
        spinner.setAdapter(spinnerAdapter);
        spinner.setSelection(0);
        spinner.setDropDownVerticalOffset(10);
    }

}
